using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

/// <summary>
/// Summary description for Category
/// </summary>
/// 
[ActiveRecord]
public class Category : ActiveRecordBase<Category>
{
    public static NHibernate.Expression.MatchMode Exact = NHibernate.Expression.MatchMode.Exact;
    public static NHibernate.Expression.MatchMode Anywhere = NHibernate.Expression.MatchMode.Anywhere;
    public static NHibernate.Expression.MatchMode End = NHibernate.Expression.MatchMode.End;
    public static NHibernate.Expression.MatchMode Start = NHibernate.Expression.MatchMode.Start;

    private int categoryID;

    [PrimaryKey(PrimaryKeyType.Identity)]
    public int CategoryID
    {
        get { return categoryID; }
        set { categoryID = value; }
    }

    private String categoryName;

    [Property]
    public String CategoryName
    {
        get { return categoryName; }
        set { categoryName = value; }
    }

    private String url;

    [Property]
    public String Url
    {
        get { return url; }
        set { url = value; }
    }

    private String categoryDescription;

    [Property]
    public String CategoryDescription
    {
        get { return categoryDescription; }
        set { categoryDescription = value; }
    }

    private String categoryFeature;

    [Property]
    public String CategoryFeature
    {
        get { return categoryFeature; }
        set { categoryFeature = value; }
    }

    private int sequence;

    [Property]
    public int Sequence
    {
        get { return sequence; }
        set { sequence = value; }
    }

    private int parentID;
    [Property]
    public int ParentID
    {
        get { return parentID; }
        set { parentID = value; }
    }

    private bool activeFlag;
    [Property]
    public bool ActiveFlag
    {
        get { return activeFlag; }
        set { activeFlag = value; }
    }

    private DateTime createdDate;
    [Property]
    public DateTime CreatedDate
    {
        get { return createdDate; }
        set { createdDate = value; }
    }

    private DateTime updatedDate;
    [Property]
    public DateTime UpdatedDate
    {
        get { return updatedDate; }
        set { updatedDate = value; }
    }

    private String createdBy;
    [Property]
    public String CreatedBy
    {
        get { return createdBy; }
        set { createdBy = value; }
    }


    private String updatedBy;
    [Property]
    public String UpdatedBy
    {
        get { return updatedBy; }
        set { updatedBy = value; }
    }

	public Category()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public Category(String categoryName,String url, String categoryDescription,String categoryFeature,int sequence,int parentid,  bool activeFlag, String createdBy)
	{
        this.categoryName = categoryName;
        this.url = url;
        this.categoryDescription = categoryDescription;
        this.categoryFeature = categoryFeature;
        this.sequence = sequence;
        this.parentID = parentid;
        this.updatedBy = createdBy;
        this.createdBy = createdBy;
        this.createdDate = DateTime.Now;
        this.updatedDate = DateTime.Now;
        this.activeFlag = activeFlag;
	}

    public static void delete_category(int categoryid)
    {
        Category category = get_category(categoryid);
        category.Delete();
    }


    public static void update_category(int categoryID, String categoryName, String url, String categoryDescription, String categoryFeature, int sequence, int parentid, bool activeFlag, String updatedBy)
    {
        Category category = new Category();
        category = get_category(categoryID);
        category.categoryName = categoryName;
        category.url = url;
        category.categoryDescription = categoryDescription;
        category.categoryFeature = categoryFeature;
        category.sequence = sequence;
        category.parentID = parentid;
        category.activeFlag = activeFlag;
        category.UpdatedBy = updatedBy;
        category.updatedDate = DateTime.Now;
        category.Update();
    }

    public static void update_category(int categoryID, String categoryName, String url, String categoryDescription, String categoryFeature, int sequence, bool activeFlag, String updatedBy)
    {
        Category category = new Category();
        category = get_category(categoryID);
        category.categoryName = categoryName;
        category.url = url;
        category.categoryDescription = categoryDescription;
        category.categoryFeature = categoryFeature;
        category.sequence = sequence;
        category.activeFlag = activeFlag;
        category.UpdatedBy = updatedBy;
        category.updatedDate = DateTime.Now;
        category.Update();
    }

    public static void update_category(int categoryID, bool activeFlag, String updatedBy)
    {
        Category category = new Category();
        category = get_category(categoryID);
        category.activeFlag = activeFlag;
        category.UpdatedBy = updatedBy;
        category.updatedDate = DateTime.Now;
        category.Update();
    }

    public static void update_category(int categoryID, int parentID, String updatedBy)
    {
        Category category = new Category();
        category = get_category(categoryID);
        category.parentID = parentID;
        category.UpdatedBy = updatedBy;
        category.updatedDate = DateTime.Now;
        category.Update();
    }

    public static void create_category(String categoryName, String url, String categoryDescription, String categoryFeature,int sequence, int parentid, bool activeFlag, String createdBy)
    {
        Category category = new Category(categoryName,url, categoryDescription,categoryFeature,sequence,parentid, activeFlag, createdBy);
        category.Create();

    }

    public static Category get_category(int categoryid)
    {
        return FindByPrimaryKey(categoryid);
    }

    public static Category get_category(string cateogoryName)
    {
        Category[] body = FindAllByProperty("CategoryName", cateogoryName);
            if (body != null && body.Length > 0)
                return body[0];
            return null;
    }

    public static Category[] get_subcategory(int parentID)
    {
        return FindAllByProperty("ParentID", parentID);        
    }

    public static Category[] get_all_category()
    {
        return FindAll();
    }

    public static int count_all_category()
    {
        return Count();
    }

    public static Category[] search_category(String propertyName, String textSearch, NHibernate.Expression.MatchMode mode)
    {
        NHibernate.Expression.DetachedCriteria criteria = NHibernate.Expression.DetachedCriteria.For<Category>();

        criteria.Add(NHibernate.Expression.Expression.InsensitiveLike(propertyName, textSearch, mode));

        return FindAll(criteria, Category.Asc(propertyName));
    }

    public static Category[] get_category_and_sort(String propertyName, String order)
    {
        NHibernate.Expression.DetachedCriteria avgWeight = NHibernate.Expression.DetachedCriteria.For<Category>();
        if (order == "ASC")
            return FindAll(avgWeight, Asc(propertyName));
        return FindAll(avgWeight, Desc(propertyName));
    }
}
